<template>
	<view>
		<view class="white"
			style="height: 90rpx;width: 670rpx;display: flex;flex-direction: row;justify-content: space-between;border: 1rpx solid #e8e8e8;border-radius: 20rpx;margin-left: 40rpx;margin-top: 30rpx;">
			<input @confirm="loadCourse(cname)" style="height: 90rpx;width: 620rpx;margin-left: 30rpx;" v-model='cname' placeholder='课程号/课程名' />
			<view style="margin-right: 10rpx;display: flex;justify-content: center;align-items: center;">
				<image style="margin-top: 5rpx;margin-right: 20rpx;height: 50rpx;width: 50rpx;"
					@click="loadCourse(cname)" src="../../static/icons/search.svg">
				</image>
			</view>
		</view>
		<view style="background-color: #fff">

		</view>
		<view
			style="width: 670rpx;padding:32rpx 40rpx;margin:20rpx 40rpx;display: flex;flex-direction: column;background-color: #fff;border-radius: 28rpx;"
			v-for="(item,index) in courseArr" :key='index'>
			<view
				style="font-size: 52rpx;font-weight: bold;width:590rpx;height: 80rpx;overflow-x:scroll;white-space: nowrap;margin-top: 18rpx;">
				{{item.name}}
			</view>
			<view style="display: flex;flex-direction: row;align-items: center;margin-top: 18rpx;">
				<image src="../../static/icons/teacher-head-fill.svg"
					style="width: 96rpx;height: 96rpx;margin-left: -10rpx;"></image>
				<view style="margin-left: 28rpx;width: 466rpx;display: flex;flex-direction: column;">
					<view style="font-size: 38rpx;">任课老师</view>
					<view style="font-size: 34rpx;color: #8a8a8d;margin-top: 4rpx;">{{item.teacher.toString()}}</view>

				</view>
			</view>
			<view style="display: flex;flex-direction: row;align-items: center;margin-top: 36rpx;">
				<image src="../../static/icons/ios-time.svg" style="width: 96rpx;height: 96rpx;margin-left: -10rpx;">
				</image>
				<view style="margin-left: 28rpx;width: 466rpx;display: flex;flex-direction: column;">
					<view style="font-size: 38rpx;">上课时段</view>
					<view v-for="(item1, index1) in item.ortp" :key="index1"
						style="font-size: 34rpx;color: #8a8a8d;margin-top: 4rpx;">
						<view>{{item1.week.toString()}} {{item1.day}} {{item1.juti}} {{item1.place}}</view>
					</view>

				</view>
			</view>
			<view style="height: 1rpx;width: 100%;background-color: #bfbfbf;margin-top: 40rpx;"></view>
			<view
				style="display: flex;flex-direction: row;width: 100%;justify-content: center;margin-top: 30rpx;color: #8a8a8d;">
				<view @click="addClassIntoTable(item)" style="display: flex;flex-direction: row;align-items: center;">
					<image src="../../static/icons/jiaHao.svg" style="width: 30rpx;height: 30rpx;opacity: 60%;"></image><text style="margin-left: 15rpx;">添加</text>
				</view>
			</view>
		</view>
		<view style="align-items: center;display: flex;justify-content: center;margin-top: 200rpx;">
			<text v-show="notAtAll" style="font-size: 150%; opacity: 0.7;color: #C6C6C8;">暂无课程</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				notAtAll: false,
				cname: "",
				form: {
					name: '',
					teachers: [],
					// 学分
					credit: 0,
					bixiu: '必修',
					time: [{
						place: '',
						week: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17],
						day: 0,
						juti: [0, 1],
					}],
					color: {
						id: 3,
						name: '蓝色',
						border_color: '#51aaf2',
						background_color: '#d6edfb',
						font_color: '#2f6691',
						fill_color: '#59b0f3',
						checked: false
					}
				},
				courseArr: []
			}
		},
		methods: {
			loadCourse(cname) {
				let _sf = this
				console.log(cname)
				uni.request({
					url: getApp().globalData.URL + 'common_apis/getClassDataById',
					// url: "http://127.0.0.1:7999/getClassData/getDataById", //获取数据的网址
					method: "POST",
					data: {
						main: cname,
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded',
					},
					success(res) {
						console.log(res)
						_sf.courseArr = res.data.main
						console.log(res.data.main)

					},
					complete() {
						if(_sf.courseArr != null){
							if (_sf.courseArr.length > 0) {
								_sf.notAtAll = false
							} else {
								_sf.notAtAll = true
							}		
						}else{
							_sf.notAtAll = true
						}

						console.log(cname)
					}
				})
			},
			addClassIntoTable(res) {
				// console.log(res)
				let _sf = this
				let addMainDef = function(res) {
					_sf.form['name'] = res.name
					let temp = {}
					_sf.form['time'] = []
					for (let i = 0; i < res.timeAndPlace.length; i++) {
						temp = {}
						temp['place'] = res.timeAndPlace[i].place
						temp['day'] = parseInt(res.timeAndPlace[i].day)
						temp['week'] = res.timeAndPlace[i].week
						temp['juti'] = res.timeAndPlace[i].juti
						_sf.form['time'].push(temp)
					}
					_sf.form['teachers'] = res.teacher
					// console.log(this.form)
					_sf.form.id = getApp().globalData.kechengbiao.length
					getApp().globalData.kechengbiao.push(_sf.form)
					uni.setStorageSync('kechengbiao', getApp().globalData.kechengbiao)

					// #ifdef APP-PLUS
					if (getApp().globalData.platform == 'ios') {
						var UserDefaultsClass = plus.ios.importClass("NSUserDefaults")
						var standardUserDefaults = UserDefaultsClass.standardUserDefaults().initWithSuiteName(
							"group.cyonline.makeabetterworld.cn")
						plus.ios.invoke(standardUserDefaults, "setObject:forKey:", JSON.stringify(getApp().globalData
							.kechengbiao.concat(
								getApp().globalData.add_event)), 'kechengbiao');
						plus.ios.invoke(standardUserDefaults, "synchronize");
					}
					// #endif

					_sf.form = {
						name: '',
						teachers: [],
						// 学分
						credit: 0,
						bixiu: '必修',
						time: [{
							place: '',
							week: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17],
							day: 0,
							juti: [0, 1],
						}],
						color: {
							id: 3,
							name: '蓝色',
							border_color: '#51aaf2',
							background_color: '#d6edfb',
							font_color: '#2f6691',
							fill_color: '#59b0f3',
							checked: false
						}
					}

					// uni.navigateBack({})
					uni.showToast({
						title: '已保存'
					})
				}
				if (res.timeAndPlace.length == 0) {
					uni.showModal({
						title: "提示",
						content: "该课程没有时段，是否添加到课程表",
						success(d) {
							if (!d.confirm) {
								return
							} else {
								addMainDef(res)
							}
						}
					})
				} else {
					addMainDef(res)
				}
			}
		}
	}
</script>

<style>
	page {
		background-color: #efefef;
	}

	.white {
		background-color: #ffffff;
	}
</style>
